package com.wangyu.generator.core.utils;


import com.wangyu.generator.core.context.GeneratorType;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Properties;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 功能描述：文件操作类
 *
 * @author: zhangQ
 * @date: 2016-8-18 15:00
 */
public class GeneratorFileUtils {

  /**
   * sl4j
   */
  private static final Logger LOGGER = LoggerFactory.getLogger(GeneratorFileUtils.class);

  /**
   * 写文件
   */
  public static void write(String content, String path) {
    try {
      OutputStreamWriter writerStream = new OutputStreamWriter(new FileOutputStream(path), "UTF-8");
      BufferedWriter writer = new BufferedWriter(writerStream);
      writer.write(content);
      writer.flush();
      writer.close();
      LOGGER.debug("生成文件完成, path=" + path);
    } catch (IOException e) {
      LOGGER.error("生成文件失败, path=" + path, e);
      throw new RuntimeException(e);
    }
  }

  /**
   * 创建Package目录文件
   */
  public static void createPackageDirectory(Properties properties, String[] layers) {
    String location = PropertiesUtils.getLocation(properties);

    String project = PropertiesUtils.getProject(properties);
    if (StringUtils.isNotBlank(project)) {
      location = location + "/" + properties.get("java.src");
    }

    String packageDir = "/" + PropertiesUtils.getPackage(properties).replaceAll("[.]", "/");
    if (StringUtils.isNotBlank(project)) {
      for (GeneratorType configDirType : GeneratorType.values()) {
        if (ArrayUtils.contains(layers, configDirType.getType())) {
          String[] targetDirs = StringUtils.split(configDirType.getTargetDir(), "\\|");
          for (String targetDir : targetDirs) {
            createDir(location + packageDir + targetDir);
          }
        }
      }
    }
  }

  /**
   * 创建目录文件
   */
  public static void createDir(String dir) {
    File file;
    file = new File(dir);
    if (!file.exists()) {
      file.mkdirs();
    }
  }

  /**
   * 获取Package目录
   */
  public static String getPackageDirectory(String name, Properties properties) {
    String location = PropertiesUtils.getLocation(properties);
    String packageDir = "/" + PropertiesUtils.getPackage(properties).replaceAll("[.]", "/");
    String project = PropertiesUtils.getProject(properties);
    String directory;
    if (StringUtils.isNotBlank(project)) {
      directory = location + "/" + properties.get("java.src") + packageDir + "/" + name + "/";
    } else {
      directory = location + packageDir + "/" + name + "/";
    }
    return directory;
  }
}